import request from "request";
import { getAll } from "./rc";
import downLoadGit from "download-git-repo";
import { downLoadDirectory } from "./constants";
import cons from "consolidate";
import axios from "axios";

let fetch = async (url) => {
	return new Promise((resolve, reject) => {
		//let config = {};
		axios
			.get(url)
			.then((res) => {
				resolve(res.data);
			})
			.catch((err) => {
				reject(err);
			});
		// request(config, (err, response, body) => {
		// 	if (err) {
		// 		reject(err);
		// 	}
		// 	console.log(body);
		// 	resolve(JSON.parse(body));
		// });
	});
};

//链接地址：https://api.github.com/repos/zhufeng-cli/vue-template/tags 版本
export let tagList = async (repo) => {
	let config = await getAll();
	let api = `https://api.github.com/repos/${config.registry}/${repo}/tags`;
	return await fetch(api);
};

//链接地址：https://api.github.com/orgs/zhufeng-cli/repos 项目
export let repoList = async () => {
	let config = await getAll();
	let api = `https://api.github.com/${config.type}/${config.registry}/repos`;
	console.log(api);
	return await fetch(api);
};

export let download = async (src, dest) => {
	return new Promise((resolve, reject) => {
		downLoadGit(src, dest, (err) => {
			if (err) {
				reject(err);
			}
			resolve();
		});
	});
};

//下载到本地
export let downloadLocal = async (project, version) => {
	let config = await getAll();
	let api = `${config.registry}/${project}`;
	if (version) {
		api += `#${version}`;
	}
	return await download(api, downLoadDirectory + "/" + project);
};
